<template>
  <div>
    <el-table :data="tableData" border stripe style="width: 100%; margin-top: 10px">
      <el-table-column label="绿植名称">
        <template slot-scope="scope">
          <a :href="['/front/publicGoods?id=' + scope.row.publicProducproId]">{{ scope.row.publicProducproName }}</a>
        </template>
      </el-table-column>
      <el-table-column prop="publicProducproImg" label="绿植图片">
        <template slot-scope="scope">
          <el-image :src="getFullImageUrl(scope.row.publicProducproImg)" :preview-src-list="[getFullImageUrl(scope.row.publicProducproImg)]" style="width: 100%; height: 100px" fit="contain"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="count" label="数量"></el-table-column>
      <el-table-column prop="totalPoints" label="消费积分"></el-table-column>
      <el-table-column prop="address" label="地区"></el-table-column>
      <el-table-column prop="createTime" label="兑换时间"></el-table-column>
    </el-table>
    <div style="margin-top: 10px; background-color: white; padding: 10px 0;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-size="pageSize"
        :page-sizes="[2, 5, 10]"
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import API from '../../utils/request'

export default {
  name: "MyPublic",
  data() {
    return {
      url: sessionStorage.getItem("url"),
      fileList: [],
      options: [],
      text: '',
      user: {},
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      entity: {},
      total: 0,
      dialogFormVisible: false
    };
  },
  created() {
    this.user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
    this.$emit('user', this.user);
    this.load()
  },
  methods: {
    getFullImageUrl(imgPath) {
      return this.url + imgPath;
    },
    fileSuccessUpload(res) {
      this.entity.file = this.url + res.data;
      this.fileList = [res.data]
      console.log(res)
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    load() {
       API.get("/system/public_order/listAll", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.text
          }
       }).then(res => {
          this.tableData = res.rows || []
          this.total = res.total
       })
    },
    add() {
      this.entity = {}
      this.fileList = []
      this.dialogFormVisible = true
    },
    edit(obj) {
      this.entity = JSON.parse(JSON.stringify(obj))
      this.fileList = []
      this.dialogFormVisible = true
    },
    save() {
      if (!this.entity.id) {
        API.post("/system/collect", this.entity).then(res => {
           if (res.code === 200) {
             this.$message({
               type: "success",
               message: "操作成功"
             })
           } else {
             this.$message({
               type: "error",
               message: res.msg
             })
           }
           this.load()
           this.dialogFormVisible = false
        })
      } else {
        API.put("/system/collect", this.entity).then(res => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "操作成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()
          this.dialogFormVisible = false
        })
      }
    },
    del(id) {
      API.delete("/system/collect/" + id).then(res => {
        this.$message({
          type: "success",
          message: "操作成功"
        })
        this.load()
      })
    }
  },
};
</script>

<style scoped>
</style>
